package com.htfyun.hardware.reflect;

import android.os.Environment;
import android.os.IBinder;
import android.util.Log;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

public class Utils {

    /*
     * 20170306,get a system property use shell cmd getprop
     */
    public static String getSystemProperty(String propName) {
        String line = null;
        BufferedReader input = null;
        try {

            Process process = Runtime.getRuntime().exec("getprop " + propName);
            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            input = new BufferedReader(ir);
            line = input.readLine();
            input.close();
            if (line.length() == 0) {
                line = null;
            }
        } catch (IOException ex) {
            Log.e("HSL", "Unable to read sysprop " + propName);
            return null;
        }
        return line;
    }

    public static boolean setSystemProperty(String propName, String value) {
        String readbackString;
        try {
            //String line = null;
            //BufferedReader input = null;
            Process process;
            //Runtime.getRuntime().exec("su");
            String cmd = "/system/bin/setprop " + propName + " " + value;
            Runtime.getRuntime().exec(cmd); // Process process =
            //InputStreamReader ir = new InputStreamReader(process.getInputStream());
            //input = new BufferedReader(ir);
            //line = input.readLine();
            //input.close();
            //Log.d("HSL", "setprop:cmd=" + cmd + ",output:" + line);
            readbackString = getSystemProperty(propName);

            if(readbackString != null &&  readbackString.equals(value)){
                return true;
            }

            Log.e("HSL", "setprop-shell:" + propName + " readback=" + readbackString);
        } catch (IOException ex) {
            Log.e("HSL", "Unable to set sysprop " + propName);
            return false;
        }
        return false;
    }

    public static boolean setSystemPropertyReflect(String propName, String value) {
        String className = "android.os.SystemProperties";
        String methodName = "set";
        Method setMethod = ClassReflect.GetClassPublicMethod(className, methodName);
        if( setMethod != null ){
            ClassReflect.invokeMethodArgs(setMethod, propName , value );

            String readbackString = getSystemProperty(propName);

            if(readbackString != null &&  readbackString.equals(value)){
                return true;
            }
            Log.e("HSL", "setprop-java:" + propName + " readback=" + readbackString);
            return false;
        } else {
            Log.d("HSL", "Got SystemProperties.set Method Failed!");
            return false;
        }
    }

    // 20180119,hsl add.
    public static IBinder getSystemServiceReflect(String service) {
        String className = "android.os.ServiceManager";
        String methodName = "getService";
        Method setMethod = ClassReflect.GetClassPublicMethod(className, methodName);
        if( setMethod != null ){
            Object result = ClassReflect.invokeMethodArgs(setMethod, service );

            Log.e("HSL", className + "." + methodName + " return:" + result + ", service = " + service);
            return (IBinder)result;
        } else {
            Log.d("HSL", className + "." + methodName + "--getMethod Failed" + ", service = " + service);
            return null;
        }
    }
}
